# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'daywidgetnewnote.ui'
#
# Created: Mon May 14 15:19:16 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from NewProduct import Ui_newProduct
from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.model.Product import Product
import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_dayWidgetNewNote(QtGui.QWidget):
    
    listOfProducts = []
    listOfWidgets = []
    lastProductY = 85
    currentTopic = ""

    '''
    dodaje produkt do listy
    '''
    def addProductToList(self):
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        s = str( self.productWidget.productComboBox.currentText())
        name, producer = s.split(', ')
        cur.execute("exec getProductCategory ?",str(name))
        rows = cur.fetchone()
        categoryName = rows[0];
        cur.execute("exec getProductMeasure ?",str(name))
        rows = cur.fetchone()
        measureName = rows[0];
        conn.close()
    
        name = self.productWidget.productComboBox.currentText()                                
        price = str(self.productWidget.productPriceLineEdit.text())
        quantity = str(self.productWidget.productQuantityLineEdit.text())
        product = Product(name,producer,categoryName,measureName,price,quantity)
        product.setId(self.productWidget.productComboBox.currentIndex()+1)
        self.listOfProducts.append(product)
        
    '''
        dodaje nowy produkt do listy i aktualizuje widok
    '''
    def addNewProduct(self):
        self.currentTopic = self.topicLineEdit.text()
        if self.productWidget.productPriceLineEdit.text() == "":
            raise Exception('Poprzedni produkt nie posiada wpisanej ceny')
        if self.productWidget.productQuantityLineEdit.text() == "":
            raise Exception('Poprzedni produkt nie posiada wpisanej ilosci')
        
        self.addProductToList()
       
        self.newNoteWidget = Ui_dayWidgetNewNote(self)
        self.newNoteWidget.setupUi(self.newNoteWidget,self.date,self.listOfProducts,self.listOfWidgets,self.databaseDir,self.currentTopic,self.login)
        self.newNoteWidget.setGeometry(QtCore.QRect(0, 0, 461, 511))
        self.newNoteWidget.setAutoFillBackground(True)
        self.newNoteWidget.show()
        self.listOfWidgets.append(self.newNoteWidget)
    
  
    
    '''
        zapisuje nowa notatke w systemie
    '''
    def save(self):
        if self.productWidget.productPriceLineEdit.text() == "":
            raise Exception('Poprzedni produkt nie posiada wpisanej ceny')
        if self.productWidget.productQuantityLineEdit.text() == "":
            raise Exception('Poprzedni produkt nie posiada wpisanej ilosci')
        self.addProductToList()
        
        topic = self.topicLineEdit.text()
        
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        day = self.date.day()
        month = self.date.month()
        year = self.date.year()
        date = str(year)+"-"+str(month)+"-"+str(day)
        cur.execute("exec addNote ?, ?, ?",date,str(topic),str(self.login))
        conn.commit()
        for product in self.listOfProducts:
            cur.execute("exec getNoteId ?, ?, ?",date,str(topic),str(self.login))
            row = cur.fetchone()
            id_note = row[0]
            cur.execute("exec addNotesProdcuts ?, ?, ?, ?",id_note,product.id,product.quantity,product.price)
            conn.commit()
        conn.close()
        
        for w in self.listOfWidgets:
            w.close()

    '''
        wygenerowane gui
    '''
    def setupUi(self, dayWidgetNewNote, date, listOfProducts, listOfWidgets,databaseDir,topic,login):
        self.login = login
        self.currentTopic = topic
        self.databaseDir = databaseDir
        self.dayWidgetNewNote = dayWidgetNewNote
        self.dayWidgetNewNote.setObjectName(_fromUtf8("dayWidgetNewNote"))
        self.dayWidgetNewNote.resize(471, 494)
        self.listOfProducts = listOfProducts
        self.listOfWidgets = listOfWidgets
        self.listOfWidgets.append(self)
        self.date = date
        
        #date
        self.horizontalLayoutWidget = QtGui.QWidget(self.dayWidgetNewNote)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 281, 41))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.dateHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.dateHorizontalLayout.setMargin(0)
        self.dateHorizontalLayout.setObjectName(_fromUtf8("dateHorizontalLayout"))
        self.dateTextLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.dateTextLabel.setObjectName(_fromUtf8("dateTextLabel"))
        self.dateHorizontalLayout.addWidget(self.dateTextLabel)
        self.dateLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.dateLabel.setObjectName(_fromUtf8("dateLabel"))
        self.dateHorizontalLayout.addWidget(self.dateLabel)
        
        #center
        self.centralScrollArea = QtGui.QScrollArea(self.dayWidgetNewNote)
        self.centralScrollArea.setGeometry(QtCore.QRect(20, 70, 431, 401))
        self.centralScrollArea.setWidgetResizable(True)
        self.centralScrollArea.setObjectName(_fromUtf8("centralScrollArea"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 429, 399))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.scrollArea = QtGui.QScrollArea(self.scrollAreaWidgetContents)
        self.scrollArea.setGeometry(QtCore.QRect(0, -10, 431, 411))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.scrollAreaWidgetContents_2 = QtGui.QScrollArea()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 429, 409))
        self.scrollAreaWidgetContents_2.setObjectName(_fromUtf8("scrollAreaWidgetContents_2"))
        
        #topic
        self.horizontalLayoutWidget_3 = QtGui.QWidget(self.scrollAreaWidgetContents_2)
        self.horizontalLayoutWidget_3.setGeometry(QtCore.QRect(10, 10, 401, 22))
        self.horizontalLayoutWidget_3.setObjectName(_fromUtf8("horizontalLayoutWidget_3"))
        self.topicHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_3)
        self.topicHorizontalLayout.setMargin(0)
        self.topicHorizontalLayout.setObjectName(_fromUtf8("topicHorizontalLayout"))
        self.topicLabel = QtGui.QLabel(self.horizontalLayoutWidget_3)
        self.topicLabel.setObjectName(_fromUtf8("topicLabel"))
        self.topicHorizontalLayout.addWidget(self.topicLabel)
        self.topicLineEdit = QtGui.QLineEdit(self.horizontalLayoutWidget_3)
        self.topicLineEdit.setObjectName(_fromUtf8("topicLineEdit"))
        self.topicHorizontalLayout.addWidget(self.topicLineEdit)
        
        
        #labels
        self.horizontalLayoutWidget_2 = QtGui.QWidget(self.scrollAreaWidgetContents_2)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(10, 50, 401, 31))
        self.horizontalLayoutWidget_2.setObjectName(_fromUtf8("horizontalLayoutWidget_2"))
        self.labelsHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.labelsHorizontalLayout.setMargin(0)
        self.labelsHorizontalLayout.setObjectName(_fromUtf8("labelsHorizontalLayout"))
        self.productLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.productLabel.setObjectName(_fromUtf8("productLabel"))
        self.labelsHorizontalLayout.addWidget(self.productLabel)
        self.quantityLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.quantityLabel.setObjectName(_fromUtf8("quantityLabel"))
        self.labelsHorizontalLayout.addWidget(self.quantityLabel)
        self.priceLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.priceLabel.setObjectName(_fromUtf8("priceLabel"))
        self.labelsHorizontalLayout.addWidget(self.priceLabel)
        
        for p in self.listOfProducts:
            self.productWidget = Ui_newProduct()
            self.productWidget.setupUi(self.scrollAreaWidgetContents_2,self.lastProductY,self.databaseDir)
            self.productWidget.productComboBox.setCurrentIndex(p.id-1)
            self.productWidget.productQuantityLineEdit.setText(p.quantity)
            self.productWidget.productPriceLineEdit.setText(p.price)
            self.lastProductY += 31

            
        self.productWidget = Ui_newProduct()
        self.productWidget.setupUi(self.scrollAreaWidgetContents_2,self.lastProductY,self.databaseDir)
        self.lastProductY += 31
        self.topicLineEdit.setText(str(self.currentTopic))
        

        #add new product button
        self.horizontalLayoutWidget_4 = QtGui.QWidget(self.scrollAreaWidgetContents_2)
        self.horizontalLayoutWidget_4.setGeometry(QtCore.QRect(10, self.lastProductY + 10, 151, 31))
        self.horizontalLayoutWidget_4.setObjectName(_fromUtf8("horizontalLayoutWidget_4"))
        self.addProductHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_4)
        self.addProductHorizontalLayout.setMargin(0)
        self.addProductHorizontalLayout.setObjectName(_fromUtf8("addProductHorizontalLayout"))
        self.addProductButton = QtGui.QPushButton(self.horizontalLayoutWidget_4)
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Maximum, QtGui.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.addProductButton.sizePolicy().hasHeightForWidth())
        self.addProductButton.setSizePolicy(sizePolicy)
        self.addProductButton.setMinimumSize(QtCore.QSize(15, 0))
        self.addProductButton.setMaximumSize(QtCore.QSize(27, 23))
        self.addProductButton.setObjectName(_fromUtf8("addProductButton"))
        self.addProductButton.connect(self.addProductButton, QtCore.SIGNAL("clicked()"),self.addNewProduct)
        self.addProductHorizontalLayout.addWidget(self.addProductButton)
        self.addProductLabel = QtGui.QLabel(self.horizontalLayoutWidget_4)
        self.addProductLabel.setObjectName(_fromUtf8("addProductLabel"))
        self.addProductHorizontalLayout.addWidget(self.addProductLabel)
        
        #save button
        self.saveButton = QtGui.QPushButton(self.scrollAreaWidgetContents_2)
        self.saveButton.setGeometry(QtCore.QRect(340, 380, 75, 23))
        self.saveButton.setObjectName(_fromUtf8("saveButton"))
        self.saveButton.connect(self.saveButton, QtCore.SIGNAL("clicked()"),self.save)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents_2)
        self.centralScrollArea.setWidget(self.scrollAreaWidgetContents)
      
        self.retranslateUi(self.dayWidgetNewNote)
        QtCore.QMetaObject.connectSlotsByName(self.dayWidgetNewNote)

    '''
        wygenerowane gui
    '''
    def retranslateUi(self, dayWidgetNewNote):
        self.dayWidgetNewNote.setWindowTitle(QtGui.QApplication.translate("dayWidgetNewNote", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.dateTextLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:18pt;\">Data: </span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.dateLabel.setText(self.date.toString("dd.MM.yyyy"))
        self.topicLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt;\">Temat: </span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.addProductButton.setText(QtGui.QApplication.translate("dayWidgetNewNote", "+", None, QtGui.QApplication.UnicodeUTF8))
        self.addProductLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Dodaj nowy produkt</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.saveButton.setText(QtGui.QApplication.translate("dayWidgetNewNote", "Zapisz", None, QtGui.QApplication.UnicodeUTF8))
        self.productLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Produkt</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.quantityLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Ilość</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.priceLabel.setText(QtGui.QApplication.translate("dayWidgetNewNote", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Cena</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))  